import {getRandom} from '../utils'
import {TileImage, XYZ as XYZSsource} from 'ol/source'
import TileGrid from 'ol/tilegrid/TileGrid'
import {get as getProjection} from 'ol/proj'
import {getTopLeft, getWidth} from 'ol/extent';

export default {
  //加载百度源
  createBDLayerSource(params) {
    const projection = getProjection('baidu');
    const resolutions = [];
    for (var i = 0; i <= 19; i++) {
      resolutions[i] = Math.pow(2, 18 - i)
    }
    return new TileImage({
      projection: projection,
      wrapX: true, // 允许左右无限移动
      tileGrid: new TileGrid({
        resolutions: resolutions,
        origin: [0, 0],
        tileSize: [256, 256]
      }),
      tileUrlFunction: (tileCoord, ss, e) => {
        if (!tileCoord) {
          return '';
        }
        let z = tileCoord[0];
        let x = tileCoord[1];
        let y = -tileCoord[2] - 1;
        // 百度瓦片服务url将负数使用M前缀来标识
        if (x < 0) {
          x = 'M' + (-x);
        }
        if (y < 0) {
          y = 'M' + (-y);
        }
        return `https://maponline${getRandom(0, 3, true)}.bdimg.com/tile/?qt=vtile&x=${x}&y=${y}&z=${z}&styles=pl&scaler=1&udt=20240425&from=jsapi2_0`
      }
    })
  },
  createBDLayerSource3(params) {
    var resolutions = []
    for (var i = 0; i <= 19; i++) {
      resolutions[i] = Math.pow(2, 18 - i)
    }
    // 创建XYZ瓦片图
    return new XYZSsource({
      projection: 'baidu',
      tileGrid: new TileGrid({
        resolutions: resolutions,
        origin: [0, 0],
        tileSize: [256, 256]
      }),
      wrapX: true, // 允许左右无限移动
      tileUrlFunction: function (tileCoord, ss, e) {
        if (!tileCoord) {
          console.log('无', tileCoord)
          return '';
        }
        let z = tileCoord[0];
        let x = tileCoord[1];
        let y = -tileCoord[2] - 1;
        // return `https://maponline0.bdimg.com/tile/?qt=vtile&x=${x}&y=${y}&z=${z}&styles=pl&scaler=1&udt=20240425&from=jsapi2_0`
        return "http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=" + x + "&y=" + y + "&z=" + z + "&styles=pl&udt=20151021&scaler=1&p=1"
      }
    })
  },
  createBDLayerSource2(params) {
    var resolutions = []
    for (var i = 0; i <= 19; i++) {
      resolutions[i] = Math.pow(2, 18 - i)
    }
    let tilegrid = new TileGrid({
      origin: [0, 0],
      resolutions: resolutions
    })
    return new XYZSsource({
      projection: 'baidu',
      maxZoom: 18,
      wrapX: true, // 允许左右无限移动
      tileUrlFunction: function (tileCoord, ss, e) {
        var x = tileCoord[1];
        var y = tileCoord[2];
        var z = tileCoord[0];
        console.log(x, y, z, ss, e)
        y = -y
        return `http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=${x}&y=${y}&z=${z}&styles=pl&udt=20220426&scaler=2&p=1`
      },
      tileGrid: new TileGrid({
        resolutions: resolutions,
        origin: [0, 0],
        tileSize: [256, 256]
      })
    })
  },
  createBDLayerSource1(params) {
    var resolutions = []
    for (var i = 0; i <= 19; i++) {
      resolutions[i] = Math.pow(2, 18 - i)
    }
    let tilegrid = new TileGrid({
      origin: [0, 0],
      resolutions: resolutions
    })
    // return new XYZSsource({
    //   projection: 'baidu',
    //   maxZoom: 18,
    //   wrapX: true, // 允许左右无限移动
    //   tileUrlFunction: function (tileCoord, ss, e) {
    //     var x = tileCoord[1];
    //     var y = tileCoord[2];
    //     var z = tileCoord[0];
    //     console.log(x, y, z, ss, e)
    //     y = -y
    //     return `http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=${x}&y=${y}&z=${z}&styles=pl&udt=20220426&scaler=2&p=1`
    //   },
    //   tileGrid: new TileGrid({
    //     resolutions: resolutions,
    //     origin: [0, 0],
    //     tileSize: [256, 256]
    //   })
    // })
    return new TileImage({
      projection: 'baidu', // 值为'BD:09'
      // projection: 'EPSG:3857', // 值为'BD:09'
      // tilePixelRatio: 2,
      tileGrid: tilegrid,
      wrapX: true, // 允许左右无限移动
      tileUrlFunction: function (tileCoord, pixelRatio, projection) {
        if (!tileCoord) {
          return "";
        }
        let z = tileCoord[0];
        let x = tileCoord[1];
        let y = -tileCoord[2] - 1;

        // 百度瓦片服务url将负数使用M前缀来标识
        if (x < 0) {
          x = 'M' + (-x);
        }
        if (y < 0) {
          y = 'M' + (-y);
        }
        // return "http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=" + x + "&y=" + y + "&z=" + z + "&styles=pl&udt=20151021&scaler=1&p=1";
        // online3的3是用来分流的，还可以是其他路径
        // udt应该表示的是地图发布日期。p表示地图上的信息，scaler表示缩放
        // 0表示不显示地名等标注信息，只单纯的地图地图，1表示显示地名信息
        // return `http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=${x}&y=${y}&z=${z}&styles=pl&udt=20220426&scaler=2&p=1`;//20190426
        // return "https://maponline3.bdimg.com/tile/?qt=vtile&x=" + x + "&y=" + y + "&z=" + z + "&styles=pl&udt=20151021&scaler=1&p=1";
        return `https://maponline1.bdimg.com/tile/?qt=vtile&x=${x}&y=${y}&z=${z}&styles=pl&scaler=1&udt=20240425&from=jsapi2_0`;
        // return "http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=" + x + "&y=" + y + "&z=" + z + "&styles=pl&udt=20151021&scaler=1&p=1";
      }
    })
  }
}

